<?php
namespace app\index\controller;

use app\BaseController;
use app\service\cxyunService;
use think\facade\Db;
use think\facade\Log;
use app\service\orderService;
use app\service\apiService;
//error_reporting(0);
class Callback extends BaseController{
    
    public function getuser(){
        $api = new apiService();
        //获取当前用户支付宝ID
        $code = request()->get('auth_code');
        $oauthtoken = $api->oauthToken($code);
        if(property_exists($oauthtoken,'sub_msg')){
            return json(['info' => $oauthtoken->sub_msg]);
        }
        
        session('user_id',$oauthtoken->user_id);
        $url=session("url");
        $this->redirect($url);
        die;    
    }
    public function getWechatUser(){
        $code = request()->get('code');
        $appid=env("APP.WECHAT_ACCOUNT_APPID");
        $secret=env("APP.WECHAT_ACCOUNT_SECRET");
        //用通过code换取网页授权access_token，同时获得openid
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
        $userinfo=getJson($url);
        //var_dump($userinfo);
//        echo session("url");
//        echo $userinfo["openid"];
        if(!empty($userinfo["openid"])){
            //echo "22";exit;
            session('wechat_openid' ,$userinfo["openid"]);
            $url=session("url");
            //echo $url;exit;
            $this->redirect($url);
            die;
            exit;
        }else{
            return "用户信息获取失败";
        }
        return "22";
    }
    public function alinotice(){
        $path=dir(getcwd())->path.'/log/'.date('Ymd')."/alinotice";
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        $address = $path."/".date('His').rand(100000,999999).'.txt';
        file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);
        parse_str(file_get_contents($address),$result);//读取文本并变成数组
        if($result['trade_status'] == 'TRADE_SUCCESS'){
            $info= Db::table("cxyun_order")->where('tradeNo',$result['trade_no'])->find();
            if(!empty($info)){
                //查询订单是否已经回调
                $billinfo=Db::table("cxyun_pay_bill")->where("trade_no",$result['trade_no'])->find();
                $parkinglotinfo=Db::table("parkinglotinfo")->where("parkingNo",$info["parkingNo"])->find();
                if(empty($billinfo)){
                    if($info["payStatus"]==0){
                        $res=Db::table("cxyun_order")
                            ->where("id",$info["id"])
                            ->update(["payStatus"=>"1","payType"=>"1","payTime"=>$result['gmt_payment']]);
                    }
                    $add["parkingId"]=$info["parkingId"];
                    $add["parkingNume"]=$info["parkingName"];
                    $add["plateNumber"]=$info["plateNumber"];
                    $add["amount"]=$info["totalFee"];

                    $add["createTime"]=date("Y-m-d H:i:s");
                    $add["inLaneCode"]=$info["inLaneCode"];
                    $add["outLaneCode"]=$info["outLaneCode"];
                    $add["payType"]=3;
                    $add["trade_no"]=$info["tradeNo"];
                    $add["out_trade_no"]=$info["outTradeNo"];
                    $add["cartypeName"]=$info["cartypeName"];
                    $add["carmodelName"]=$info["carmodelName"];
                    $add["parkingNo"]=$info["parkingNo"];
                    $add["scenes"]=$info["scenes"];
                    $add["inTime"]=$info["inTime"];
                    $add["stopTime"]=$info["stopTime"];
                    $add["cxyun_order_id"]=$info["id"];
                    $add["outTime"]=date("Y-m-d H:i:s");
                    $add["orderNo"]=$info["orderNo"];
                    //`cartypeName``carmodelName` `plateColor`
                    Db::table("cxyun_pay_bill")->insertGetId($add);
                    $payNotice["parkingId"]=$info["parkingId"];
                    $payNotice["plateNumber"]=$info["plateNumber"];
                    $payNotice["payFee"]=$info["payFee"];
                    $payNotice["orderNo"]=$info["orderNo"];
                    $payNotice["payType"]=3;
                    $payNotice["timestamp"]=time()."000";
                    $payNotice["orderId"]=$info["tradeNo"];
                    $cxyunApi=new cxyunService($parkinglotinfo["cxyun_appkey"]);
                    $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
                }else{
                    $payNotice["parkingId"]=$info["parkingId"];
                    $payNotice["plateNumber"]=$info["plateNumber"];
                    $payNotice["payFee"]=$info["payFee"];
                    $payNotice["orderNo"]=$info["orderNo"];
                    $payNotice["payType"]=3;
                    $payNotice["timestamp"]=time()."000";
                    $payNotice["orderId"]=$info["tradeNo"];
                    $cxyunApi=new cxyunService($parkinglotinfo["cxyun_appkey"]);
                    $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());

                }
            }
        }
    }

    public function wxnotice(){
        $path=dir(getcwd())->path.'/log/'.date('Ymd')."/wxnotice";
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        $address = $path."/".date('His').rand(100000,999999).'.txt';
        file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);//将数据读入文本
        $xmldata = file_get_contents($address);
        //将xml数据转化为数组
        $result = json_decode(json_encode(simplexml_load_string($xmldata, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
            //$result['out_trade_no']
            $info= Db::table("cxyun_order")->where('outTradeNo',$result['out_trade_no'])->find();
            if(!empty($info)){
                //查询订单是否已经回调
                $billinfo=Db::table("cxyun_pay_bill")->where('out_trade_no',$result['out_trade_no'])->find();
                $parkinglotinfo=Db::table("parkinglotinfo")->where("parkingNo",$info["parkingNo"])->find();
                if(empty($billinfo)){
                    if($info["payStatus"]==0){
                        $res=Db::table("cxyun_order")
                            ->where("id",$info["id"])
                            ->update(["payStatus"=>"1","payType"=>"1","payTime"=>$result['time_end']]);
                    }
                    $add["parkingId"]=$info["parkingId"];
                    $add["parkingNume"]=$info["parkingName"];
                    $add["plateNumber"]=$info["plateNumber"];
                    $add["amount"]=$info["totalFee"];

                    $add["createTime"]=date("Y-m-d H:i:s");
                    $add["inLaneCode"]=$info["inLaneCode"];
                    $add["outLaneCode"]=$info["outLaneCode"];
                    $add["payType"]=2;
                    $add["trade_no"]=$info["tradeNo"];
                    $add["out_trade_no"]=$info["outTradeNo"];
                    $add["cartypeName"]=$info["cartypeName"];
                    $add["carmodelName"]=$info["carmodelName"];
                    $add["scenes"]=$info["scenes"];
                    $add["parkingNo"]=$info["parkingNo"];
                    //`cartypeName``carmodelName` `plateColor`
                    $add["inTime"]=$info["inTime"];
                    $add["stopTime"]=$info["stopTime"];
                    $add["cxyun_order_id"]=$info["id"];
                    $add["outTime"]=date("Y-m-d H:i:s");
                    $add["orderNo"]=$info["orderNo"];
                    Db::table("cxyun_pay_bill")->insertGetId($add);
                    $payNotice["parkingId"]=$info["parkingId"];
                    $payNotice["plateNumber"]=$info["plateNumber"];
                    $payNotice["payFee"]=$info["payFee"];
                    $payNotice["orderNo"]=$info["orderNo"];
                    $payNotice["payType"]=2;
                    $payNotice["timestamp"]=time()."000";
                    $payNotice["orderId"]=$info["tradeNo"];
                    $cxyunApi=new cxyunService($parkinglotinfo["cxyun_appkey"]);
                    $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
                }else{
                    $payNotice["parkingId"]=$info["parkingId"];
                    $payNotice["plateNumber"]=$info["plateNumber"];
                    $payNotice["payFee"]=$info["payFee"];
                    $payNotice["orderNo"]=$info["orderNo"];
                    $payNotice["payType"]=2;
                    $payNotice["timestamp"]=time()."000";
                    $payNotice["orderId"]=$info["tradeNo"];
                    $cxyunApi=new cxyunService($parkinglotinfo["cxyun_appkey"]);
                    $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
                }
            }

        }
    }
    public function wxnoticeTest(){
        $path=dir(getcwd())->path.'/log/'.date('Ymd')."/wxnotice";
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        $address = $path."/225419363539.txt";
        //file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);//将数据读入文本
        $xmldata = file_get_contents($address);
        //将xml数据转化为数组
        $result = json_decode(json_encode(simplexml_load_string($xmldata, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
            //$result['out_trade_no']
            $info= Db::table("cxyun_order")->where('outTradeNo',$result['out_trade_no'])->find();
            if(!empty($info)){
                //查询订单是否已经回调
                $billinfo=Db::table("cxyun_pay_bill")->where('out_trade_no',$result['out_trade_no'])->find();
                $parkinglotinfo=Db::table("parkinglotinfo")->where("parkingNo",$info["parkingNo"])->find();
                if(empty($billinfo)){
                    if($info["payStatus"]==0){
                        $res=Db::table("cxyun_order")
                            ->where("id",$info["id"])
                            ->update(["payStatus"=>"1","payType"=>"1","payTime"=>$result['time_end']]);
                    }
                    $add["parkingId"]=$info["parkingId"];
                    $add["parkingNume"]=$info["parkingName"];
                    $add["plateNumber"]=$info["plateNumber"];
                    $add["amount"]=$info["totalFee"];

                    $add["createTime"]=date("Y-m-d H:i:s");
                    $add["inLaneCode"]=$info["inLaneCode"];
                    $add["outLaneCode"]=$info["outLaneCode"];
                    $add["payType"]=2;
                    $add["trade_no"]=$info["tradeNo"];
                    $add["out_trade_no"]=$info["outTradeNo"];
                    $add["cartypeName"]=$info["cartypeName"];
                    $add["carmodelName"]=$info["carmodelName"];
                    $add["scenes"]=$info["scenes"];
                    //`cartypeName``carmodelName` `plateColor`
                    Db::table("cxyun_pay_bill")->insertGetId($add);
                    $payNotice["parkingId"]=$info["parkingId"];
                    $payNotice["plateNumber"]=$info["plateNumber"];
                    $payNotice["payFee"]=$info["payFee"];
                    $payNotice["orderNo"]=$info["orderNo"];
                    $payNotice["payType"]=2;
                    $payNotice["timestamp"]=time()."000";
                    $payNotice["orderId"]=$info["tradeNo"];
                    $cxyunApi=new cxyunService($parkinglotinfo["cxyun_appkey"]);
                    $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
                }else{
                    $payNotice["parkingId"]=$info["parkingId"];
                    $payNotice["plateNumber"]=$info["plateNumber"];
                    $payNotice["payFee"]=$info["payFee"];
                    $payNotice["orderNo"]=$info["orderNo"];
                    $payNotice["payType"]=2;
                    $payNotice["timestamp"]=time()."000";
                    $payNotice["orderId"]=$info["tradeNo"];
                    $cxyunApi=new cxyunService($parkinglotinfo["cxyun_appkey"]);
                    $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
                }
            }

        }
    }
}
